package com.watson.demo.mybatis.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * // todo
 *
 * @author: llun
 * @date: 2022/2/14 10:52
 * @version: 1.0
 */

public class DbContextHolder {

    private static Logger log = LoggerFactory.getLogger(DbContextHolder.class);
    public static final String WRITE = "write";
    public static final String READ = "read";

    private static ThreadLocal<String> threadLocal= new ThreadLocal<>();

    public static void setDbType(String dbType) {
        if (dbType == null) {
            log.error("dbType为空");
            throw new NullPointerException();
        }
        log.info("设置dbType为：{}",dbType);
        threadLocal.set(dbType);
    }

    public static String getDbType() {
        return threadLocal.get() == null ? WRITE : threadLocal.get();
    }

    public static void clearDbType() {
        threadLocal.remove();
    }
}
